{% macro attachment(image, user=None, has_form=True) -%}
  <div class="attachment">
    {% if user and (user.id == image.creator_id or
                    user.has_perm('upload.delete_imageattachment')) %}
      {% if has_form %}
        <form class="delete" method="post"
              action="{{ url('upload.del_image_async', image.pk) }}">
          {% csrf_token %}
          <input type="submit" name="delete" class="delete"
                 data-url="{{ url('upload.del_image_async', image.pk) }}"
                 title="{{ _('Delete this image') }}" value="✖"/>
        </form>
      {% else %}
        <input type="submit" class="delete" name="delete"
               data-url="{{ url('upload.del_image_async', image.pk) }}"
               title="{{ _('Delete this image') }}" value="✖"/>
        <noscript>
          <label>{{ _('Delete this image') }}
            <input type="checkbox" name="delete_image" value="{{ image.pk }}"/>
          </label>
        </noscript>

      {% endif %}
    {% endif %}
    <a class="image" href="{{ image.file.url }}">
      <img src="{{ image.thumbnail_if_set().url }}"/>
    </a>
  </div>
{%- endmacro %}

{% macro attachments_form(model_name, object_id, images, settings, user=None) -%}
  <div class="attachments-upload"
       data-post-url={{ url('upload.up_image_async', model_name, object_id)}}>
    <div class="uploaded{% if not images %} empty{% endif %}">
      <div>{{ _('Uploaded images:') }}</div>
      <div class="attachments attachments-list">
        {% for image in images %}
          {{ attachment(image, user, False) }}
        {% endfor %}
        <div class="upload-progress"
             style="height:{{ settings.THUMBNAIL_SIZE }}px;width:{{ settings.THUMBNAIL_SIZE }}px"></div>
      </div>
      <noscript>
        <input type="submit" name="delete_images" class="btn image-delete"
               value="{{ _('Delete selected images') }}"/>
      </noscript>
    </div>
    <div class="add-attachment">
      <label for="id_image">{{ _('Add images:') }}</label>
      <input type="file" id="id_image" name="image" size="30"
             accept="{{ settings.IMAGE_ALLOWED_MIMETYPES }}"
             title="{{ _('Browse for an image to upload.') }}"/>
      <noscript>
        <input type="submit" name="upload_image" class="btn"
               value="{{ _('Upload') }}"/>
      </noscript>
    </div>
    <div class="adding-attachment"></div>
  </div>
{%- endmacro %}
